package org.lex.explorer;

import java.io.File;

import javax.swing.filechooser.FileView;

public class FileInfo extends SimpleFileInfo {
	public static final FileInfo FAKE = new FileInfo();

	public static FileInfo get(FileView view, File file) {
		FileInfo info = new FileInfo();
		FileInfo.get(view, file, info);
		return info;
	}

	protected static void get(FileView view, File file, FileInfo info) {
		SimpleFileInfo.get(view, file, info);
		if (info.isFile())
			info.size = file.length();
		info.type = view.getTypeDescription(file);
		info.date = file.lastModified();
		info.attribute = Attribute.get(file);
	}

	public Attribute getAttribute() {
		return attribute;
	}

	public long getDate() {
		return date;
	}

	public long getSize() {
		return size;
	}

	public String getType() {
		return type;
	}

	private long size = 0L;
	private String type = "";
	private long date = 0L;
	private Attribute attribute = null;

	public static class Attribute {
		public static Attribute get(File file) {
			Attribute a = new Attribute();
			a.readable = file.canRead();
			a.writable = file.canWrite();
			a.executable = file.canExecute();
			return a;
		}

		private boolean readable = false;
		private boolean writable = false;
		private boolean executable = false;

		public String toString() {
			StringBuilder buf = new StringBuilder(3);
			if (readable)
				buf.append('R');
			if (writable)
				buf.append('W');
			if (executable)
				buf.append('X');
			return buf.toString();
		}
	}
}
